/**
 * ajax post方法
 * */
function ajaxPost(url, postData, successFun, failFun, isMore, def) {
	$.ajax({
		url: url,
		cache: false,
		data: JSON.stringify(postData),
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		contentType: "application/json",
		timeout: 10000, //超时时间设置为10秒；
		success: function(res) {
			console.log(res)
		    $.hideLoading();
			if(res.success) {
				if(successFun) {
					if(isMore) {
						successFun(res, isMore);
					} else {
						successFun(res);
					}
					if(def) {
						def.resolve();
					}
				}
			} else {
				if(failFun) {
					failFun(res);
				} else {
					$.toast(res.msg, "text");
				}
			}
		},
		error: function(res) {
		  $.hideLoading();
			// $.toast("网络出错……", "text");
			if (failFun) {
        failFun({msg: "网络出错"});
      } else {
        $.toast("网络出错", "text");
      }
		}
	});
}

//Promise
function ajaxPost2(url, postData, resolve, reject) {
	$.showLoading();
	$.ajax({
		url: url,
		cache: false,
		data: JSON.stringify(postData),
		dataType: 'json', //服务器返回json格式数据
		type: 'post', //HTTP请求类型
		contentType: "application/json",
		timeout: 10000, //超时时间设置为10秒；
		success: function(res) {
			console.log(res)
			$.hideLoading();
			if(res.success) {
				resolve(res);
			} else {
				reject(res);
			}
		},
		error: function(res) {
			$.hideLoading();
			reject({
				code: -999,
				msg: "网络出错……",
			})
		}
	});
}

/**
 * ajax get方法
 * */
function ajaxGet(url,resolve, reject) {
	$.ajax({
		url: url,
		cache: false,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		contentType: "application/json",
		timeout: 10000, //超时时间设置为10秒；
		success: function(res) {
			console.log(res)
			$.hideLoading();
			if(res.success) {
				resolve(res);
			} else {
				reject(res);
			}
		},
		error: function(res) {
			$.hideLoading();
			reject({
				code: -999,
				msg: "网络出错……",
			})
		}
	});
}
//Promise
function ajaxGet2(url,resolve, reject) {

	$.ajax({
		url: url,
		cache: false,
		dataType: 'json', //服务器返回json格式数据
		type: 'get', //HTTP请求类型
		contentType: "application/json",
		timeout: 10000, //超时时间设置为10秒；
		success: function(res) {
			console.log(res);
			$.hideLoading();
			if(res.success) {
				resolve(res);
			} else {
				reject(res);
			}
		},
		error: function(res) {
			$.hideLoading();
			reject({
				code: -999,
				msg: "网络出错……",
			})
		}
	});
}

var ajax = {
	
  showError: function(err) {
  	var msg = '';
  	if (err instanceof Error) {
  		msg = err.message;
  	} else if (typeof(err) == 'object') {
  		msg = err.msg;
  	} else {
  		msg = err;
  	}
  	$.toast(msg, "text");
  },
	
	getCode: function(phone, successFun, failFun) {
		var url = $webRoot + "/wxsite/sms/send.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&mobile=" + phone;
		ajaxGet(url, successFun, failFun);
  },
  
	/**
	 * 登录
	 * */
	login: function(postData, successFun, failFun) {
		var url = $webRoot + "/graduation/mbr/login.do";
		ajaxPost(url, postData, successFun, failFun);
	},
	
	/**
	 * 退出
	 * */
	quit: function(successFun) {
		var url = $webRoot + "/wxsite/mbr/logout.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, '', successFun);
	},
	/**
	 * 完善修改个人信息
	 * */
	selfInfo: function(postData, successFun, failFun) {
		var url = $webRoot + "/wxsite/mbr/update.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun, failFun);
	},
	
	selfInfo2: function(postData, activityId,resolve, reject) {
		var url = $webRoot + "/wxsite/mbr/update.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		return new Promise(function(resolve, reject) {
			return ajaxPost2(url,postData,resolve, reject);
		})
	},
	
	/**
	 * 获取商户列表
	 * */
	getTenant: function(postData, successFun, isMore) {
		var def = $.Deferred();
		var url = $webRoot + "/wxsite/tenant/query.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun, '', isMore, def);
		return def;
	},
	/**
	 * 上传拍照小票接口
	 * */
	uploadTicket: function(postData, successFun) {
		var url = $webRoot + "/wxsite/score/saveTicket.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun);
	},
	/**
	 * 签到接口
	 * */
	handleSign: function(successFun, failFun) {
		var url = $webRoot + "/wxsite/mbr/signin.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 积分商城积分兑换接口
	 * */
	excScore: function(goodId, successFun, failFun) {
		var url = $webRoot + "/wxsite/score/exchange.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&goodId=" + goodId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 积分商城点击兑换，根据id获取商品详情
	 * */
	goodsDetails: function(goodId, successFun) {
		var url = $webRoot + "/wxsite/score/goodDetails.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&goodId=" + goodId;
		ajaxGet(url, successFun);
	},
	/**
	 * 商户详情点赞接口
	 * */
	handlePraise: function(tenantId, successFun, failFun) {
		var url = $webRoot + "/wxsite/tenant/praise.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&tenantId=" + tenantId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 商户详情取消点赞接口
	 * */
	handleUnpraise: function(tenantId, successFun, failFun) {
		var url = $webRoot + "/wxsite/tenant/unpraise.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&tenantId=" + tenantId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 商户详情评分接口
	 * */
	handleMark: function(tenantId, mark, successFun, failFun) {
		var url = $webRoot + "/wxsite/tenant/markit.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&id=" + tenantId + "&mark=" + mark;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 商户详情收藏接口
	 * */
	handleSubscribe: function(tenantId, successFun, failFun) {
		var url = $webRoot + "/wxsite/tenant/subscribe.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&id=" + tenantId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 商户详情取消收藏接口
	 * */
	handleUnsubscribe: function(tenantId, successFun, failFun) {
		var url = $webRoot + "/wxsite/tenant/unsubscribe.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&id=" + tenantId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 积分商城查询接口
	 * */
	queryScoreMall: function(postData, successFun, failFun) {
		var url = $webRoot + "/wxsite/score/query.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun, failFun);
	},
	/**
	 * 获取标签接口
	 * */
	getTag: function(successFun, failFun) {
		var url = $webRoot + "/wxsite/tag/getTags.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 保存标签接口
	 * */
	saveTag: function(postData, successFun, failFun) {
		var url = $webRoot + "/wxsite/tag/saveMbrTags.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun, failFun);
	},
	savTag2: function(postData){
		var url = $webRoot + "/wxsite/tag/saveMbrTags.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		return new Promise(function(resolve, reject) {
			return ajaxPost2(url,postData,resolve, reject);
		})
	},
	/**
	 * 微信分享成功后调用接口
	 * */
	sharePage: function(postData, successFun, failFun) {
		var url = $webRoot + "/wxsite/mbr/sharePage.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun, failFun);
	},
	/**
	 * 获取当前活动可领的券
	 * */
	getCanAssignCoupon: function(mallActivityId, successFun, failFun) {
		var url = $webRoot + "/wxsite/coupon/getCouponActivities.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&mallActivityId=" + mallActivityId;
		ajaxGet(url, successFun, failFun);
	},
	getCanAssignCoupon1: function(activityName,typeName,successFun, failFun) {
		var search = '';//搜索内容
		var screenType = '';//筛选类型
		if(activityName){
			search = "&activityName="+activityName;
		}
		if(typeName){
			screenType = "&typeName="+typeName;
		}
		var url = $webRoot + "/wxsite/coupon/getCouponActivities.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + search + screenType;
		ajaxGet(url, successFun, failFun);
	},
	getCouponActivitiesByFilter: function(postData,successFun,failFun){
		var url = $webRoot + "/wxsite/coupon/getCouponActivitiesByFilter.do?";
		ajaxPost(url,postData,successFun,failFun);
	},
	/*
	 * 券活动页面取得所有券类型
	 * */
	getCouponAllTypes: function(successFun,failFun){
		var url = $webRoot + "/wxsite/coupon/getType.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxGet(url,successFun,failFun)
	},
	
	/**
	 * 券活动页面领取券接口
	 * */
	getAcitivityCoupon: function(activityId,mallActivityId,successFun, failFun) {
		var url = $webRoot + "/wxsite/coupon/assign.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&activityId=" + activityId+ "&mallActivityId=" + mallActivityId;
		ajaxGet(url, successFun, failFun);
	},
	
	/**
	 * 调查问券获取问题接口
	 * */
	getQuestion: function(id, successFun, failFun) {
		var url = $webRoot + "/wxsite/paper/get.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId + "&id=" + id;
		ajaxGet(url, successFun, failFun);
	},
	/**
	 * 调查问券提交问题接口
	 * */
	submitQuestion: function(postData, successFun, failFun) {
		var url = $webRoot + "/wxsite/paper/submit.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxPost(url, postData, successFun, failFun);
	},
	
	/*
	 * 查询该商场所有楼层（没有区域）
	 * */
	getFloors: function(successFun, failFun){
		var def = $.Deferred();
		var url = $webRoot + "/wxsite/floor/getFloors.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxGet(url,successFun, failFun);
		return def;
	},
	
	/*
	 * 按区域查询所有楼层
	 * */
	getDistrictFloors: function(successFun, failFun){
		var def = $.Deferred();
		var url = $webRoot + "/wxsite/floor.do?getDistrictFloors&wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxGet(url,successFun, failFun);
		return def;
	},
	
	
	/*
	 * 按楼层查询该层的商户
	 * */
	getFloorTenants: function(floorId,successFun, failFun){
		var def = $.Deferred();
		var url = $webRoot + "/wxsite/tenant/getFloorTenants.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId +"&floorId="+ floorId;
		ajaxGet(url,successFun,failFun);
		return def;
	},
	
	/*
	 *预约接口
	 */
	getappoint: function(activityId,successFun,failFun){
		var def = $.Deferred();
		var url = $webRoot + "/wxsite/appoient/appoint.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId +"&activityId="+ activityId;
		ajaxGet(url,successFun,failFun);
		return def;
	},
	
	
	/**
	 * 获取当前用户的积分和卡券
	 * */
	getScoreCoupon: function(successFun,failFun){
		var url = $webRoot + "/wxsite/mbr/getCurrentMember.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		ajaxGet(url,successFun,failFun);
	},
	
	/**
	 * 查询积分
	 * */
	queryScoreHst: function(postData){
		var url = $webRoot + "/wxsite/score/queryScoreHst.do?wxpubid=" + $wxPubId + "&wxopenid=" + $wxOpenId;
		return new Promise(function(resolve,reject){
			 return ajaxPost2(url,postData,resolve,reject)
		})
	},


}